﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using KishOnlineDomain.Domain;
using KishOnlineDomain.Common;
using KishOnlineCommone;

namespace KishOnlineDomain.Controller
{
    public class UserController
    {
        public ResultDTO RegisterUser(UserDTO User)
        {
            ResultDTO Result = new ResultDTO();
            try
            {
                using (var scope = new TransactionScope())
                {
                    var CreateUserResult = BaseUser.CreateUser(User);
                    if (CreateUserResult.IsSucceed.HasValue && CreateUserResult.IsSucceed.Value)
                    {
                        ContactMessage Email = new ContactMessage();
                        Email.subject = Common.Static.SubjectOfUserRegistrationEmail;
                        Email.body = Common.Static.BodyUserRegistrationEmail + "<br/>نام کاربری : " + User.Email + "<br/>کلمه عبور : " + User.Password;
                        Email.to = User.Email;
                        var SendEmailResult = Utility.SendEmail(Email);
                        if (SendEmailResult.IsSucceed.HasValue && SendEmailResult.IsSucceed.Value)
                        {
                            User.ID = CreateUserResult.Data;
                            Common.Utility.SetLogginSession(User);
                            scope.Complete();
                            Result.IsSucceed = CreateUserResult.IsSucceed;
                            Result.Message = CreateUserResult.Message;
                            return Result;
                        }
                        else
                        {
                            scope.Dispose();
                            return SendEmailResult;
                        }
                    }
                    else
                    {
                        scope.Dispose();
                        Result.IsSucceed = CreateUserResult.IsSucceed;
                        Result.Message = CreateUserResult.Message;
                        return Result;
                    }
                }
            }
            catch (Exception ex)
            {
                Result = Utility.logError(ex, 8, System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.FullName);
            }

            return Result;
        }

        public static UserDTO GetCurrentUser()
        {
            return KishOnlineDomain.Common.Utility.GetLogginSession();
        }

        private BaseUser GetUserClass()
        {
            if (System.Web.HttpContext.Current.Session["User"] != null && System.Web.HttpContext.Current.Session["User"] is UserDTO)
            {
                if ((System.Web.HttpContext.Current.Session["User"] as UserDTO).Roles.Contains(RoleTypes.Admin))
                {
                    return new Admin();
                }
                else if ((System.Web.HttpContext.Current.Session["User"] as UserDTO).Roles.Contains(RoleTypes.Memebr))
                {
                    return new Member();
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public ResultDTO DeleteUser(int UserID)
        {
            ResultDTO result = new ResultDTO();
            try
            {
                BaseUser User = GetUserClass();
                result.IsSucceed = User.DeleteUser(UserID);
                result.Message = MessageReader.Get(MessageType.Event, 7);
            }
            catch (Exception ex)
            {
                result = Utility.logError(ex, 18, System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.FullName);
            }
            return result;
        }

        public ResultDTO<UserDTO> UpdateUser(UserDTO UserInfo)
        {
            ResultDTO<UserDTO> result = new ResultDTO<UserDTO>();
            try
            {
                BaseUser User = GetUserClass();
                using (var scope = new TransactionScope())
                {
                    result = User.UpdateUser(UserInfo);
                    if (result.IsSucceed.HasValue && result.IsSucceed.Value)
                    {
                        ContactMessage Email = new ContactMessage();
                        Email.subject = Common.Static.SubjectOfUserRegistrationEmail;
                        Email.body = Common.Static.BodyUserUpdateEmail + "<br/>نام کاربری : " + UserInfo.Email + "<br/>کلمه عبور : " + UserInfo.Password;
                        Email.to = UserInfo.Email;
                        var SendEmailResult = Utility.SendEmail(Email);
                        if (SendEmailResult.IsSucceed.HasValue && SendEmailResult.IsSucceed.Value)
                        {
                            Common.Utility.SetLogginSession(UserInfo);
                            scope.Complete();
                            return result;
                        }
                        else
                        {
                            scope.Dispose();
                            return new ResultDTO<UserDTO>() { Data = UserInfo, IsSucceed = SendEmailResult.IsSucceed, Message = SendEmailResult.Message };
                        }
                    }
                    else
                    {

                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
                var Message = Utility.logError(ex, 0, System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.FullName);
                result.Data = null;
                result.IsSucceed = Message.IsSucceed;
                result.Message = Message.Message;

            }
            return result;
        }

        public ResultDTO<UserDTO> GetUser(int UserID)
        {
            ResultDTO<UserDTO> result = new ResultDTO<UserDTO>();
            try
            {
                BaseUser User = GetUserClass();
                result = User.GetUser(UserID);
            }
            catch (Exception ex)
            {
                var Message = Utility.logError(ex, 24, System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.FullName);
                result.Data = null;
                result.IsSucceed = Message.IsSucceed;
                result.Message = Message.Message;
            }
            return result;
        }

        public ResultDTO<List<UserDTO>> GetAllUser()
        {
            ResultDTO<List<UserDTO>> result = new ResultDTO<List<UserDTO>>();
            try
            {
                BaseUser User = GetUserClass();
                result.Data = User.GetAllUser();
                result.IsSucceed = true;
            }
            catch (Exception ex)
            {
                var Message = Utility.logError(ex, 24, System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.FullName);
                result.Data = null;
                result.IsSucceed = Message.IsSucceed;
                result.Message = Message.Message;
            }
            return result;
        }

        public ResultDTO Loggin(string UserName, string Password)
        {
            ResultDTO result = new ResultDTO();
            try
            {
                result = BaseUser.Loggin(UserName, Password);
            }
            catch (Exception ex)
            {
                result = Utility.logError(ex, 3, System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.FullName);
            }
            return result;
        }

        public ResultDTO LoggOut()
        {
            ResultDTO result = new ResultDTO();
            try
            {
                BaseUser User = GetUserClass();
                result.IsSucceed = User.LoggOut();
                result.Message = MessageReader.Get(MessageType.Event, 16);
            }
            catch (Exception ex)
            {
                result = Utility.logError(ex, 31, System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.FullName);
            }
            return result;
        }
    }
}
